<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">

          <el-form-item label="时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>

          <el-form-item label="" prop="channel" v-if="!companyShow">
            <el-select
              v-model="queryParams.channel"
              placeholder="渠道名称"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in channelList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="" prop="companyId" v-if="!companyShow">
            <el-select
              v-model="queryParams.companyId"
              placeholder="商户号"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in companyList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table border v-loading="loading" :data="list">
          <el-table-column label="放款笔数" align="center" prop="num"/>
          <el-table-column label="放款本金" align="center" prop="money"/>
          <el-table-column label="白户数" align="center" prop="whiteNum"/>
          <el-table-column label="白户占比" align="center" prop="whiteNum">
            <template slot-scope="scope">
              <span v-if="scope.row.num == 0">0.00%</span>
              <span v-else>{{ scope.row.whiteNum / scope.row.num | numFilter }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="合同金额" align="center" prop="applyMoney"/>
          <el-table-column label="实际还款金额" align="center" prop="repayMoney"/>
          <el-table-column label="实还订单" align="center" prop="repapyNum"/>
          <el-table-column label="盈亏差额" align="center" prop="repayMoney">
            <template slot-scope="scope">
              {{ scope.row.repayMoney - scope.row.money }}
            </template>
          </el-table-column>
          <el-table-column label="逾期率" align="center" prop="overNum">
            <template slot-scope="scope">
              <span v-if="scope.row.num == 0">0.00%</span>
              <span v-else>{{ scope.row.overNum / scope.row.num | numFilter }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="本金回收率" align="center" prop="repayMoney">
            <template slot-scope="scope">
              <span v-if="scope.row.money == 0">0.00%</span>
              <span v-else>{{ scope.row.repayMoney / scope.row.money | numFilter }}%</span>

            </template>
          </el-table-column>
        </el-table>

        <el-table v-loading="loading" :data="indexList" style="margin-top: 10px;">
          <el-table-column label="日期" align="center" prop="buildDate"/>
          <el-table-column label="放款笔数" align="center" prop="num"/>
          <el-table-column label="放款本金" align="center" prop="money"/>
          <el-table-column label="白户数" align="center" prop="whiteNum"/>
          <el-table-column label="白户用户数" align="center" prop="whiteNum"/>
          <el-table-column label="白户占比" align="center" prop="whiteNum">
            <template slot-scope="scope">
              <span v-if="scope.row.num == 0">0.00%</span>
             <span v-else>{{ scope.row.whiteNum / scope.row.num | numFilter }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="合同金额" align="center" prop="applyMoney"/>
          <el-table-column label="首次逾期率" align="center" prop="firstNum">
            <template slot-scope="scope">
              <span v-if="scope.row.num == 0">0.00%</span>
              <span v-else>{{ scope.row.firstNum / scope.row.num | numFilter }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="逾期率" align="center" prop="overNum">
            <template slot-scope="scope">
              <span v-if="scope.row.num == 0">0.00%</span>
              <spa v-else>{{ scope.row.overNum / scope.row.num | numFilter }}%</spa>
            </template>
          </el-table-column>
          <el-table-column label="实际还款金额" align="center" prop="repayMoney"/>
          <el-table-column label="还款订单数" align="center" prop="repapyNum"/>
          <el-table-column label="盈亏差额" align="center" prop="repayMoney">
            <template slot-scope="scope">
              <span v-if="scope.row.money == 0">0.00%</span>
              <span v-else>{{ scope.row.repayMoney - scope.row.money }}</span>
            </template>
          </el-table-column>
          <el-table-column label="本金收回率" align="center" prop="repayMoney">
            <template slot-scope="scope">
              <span v-if="scope.row.money == 0">0.00%</span>
              <span v-else>{{ scope.row.repayMoney / scope.row.money | numFilter }}%</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

  </div>
</template>

<script>
import {listIncomeExpendData, listIncomeExpendTotalData} from "@/api/app/data/incomeExpend";
import {list, listCompany} from "@/api/company"
import {channelList} from "@/api/app/channel"

export default {
  name: "IncomeExpend",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      companyShow: this.$store.state.user.companyId,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      indexList: null,
      list: null,
      channelList: null,
      companyList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
    };
  },
  watch: {},
  created() {
    this.getList();
    this.getTotalList();
    this.getChannelList();
    this.getCompanyList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listIncomeExpendData(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.indexList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    getTotalList() {
      if (this.dateRange) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      } else {
        this.queryParams.beginTime = undefined;
        this.queryParams.endTime = undefined;
      }
      listIncomeExpendTotalData(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.list = response.data;
        }
      );
    },

    getChannelList() {
      channelList().then(response => {
          this.channelList = response.data;
        }
      );
    },

    getCompanyList() {
      list().then(response => {
          this.companyList = response.data;
        }
      );
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getTotalList();
    },


    handleMoney() {
      this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
    }
  },
  filters: {
    numFilter: (value) => {
      return value = parseFloat(value * 100).toFixed(2)
    },
    numFilter1: (value) => {
      return value = parseFloat(value).toFixed(0)
    }
  }
};
</script>
